package org.hbkj.springbootmybatis.controller;

import org.hbkj.springbootmybatis.entity.BuyDo;
import org.hbkj.springbootmybatis.service.BuyService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 购物车和购买操作的接口控制器
 */
@RestController
@RequestMapping("/api/buy")
public class BuyController {

    private final BuyService buyService;

    public BuyController(BuyService buyService) {
        this.buyService = buyService;
    }

    /**
     * 加入购物车接口
     */
    @PostMapping("/shoppingcart/add")
    public ResponseEntity<Map<String, Object>> addToCart(@RequestBody BuyDo buyDo) {
        Map<String, Object> result = new HashMap<>();

        try {
            int rows = buyService.addToCart(buyDo);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "加入购物车成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "加入购物车失败");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
            }
        } catch (IllegalArgumentException e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统异常，请稍后重试");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 立即购买接口
     */
    @PostMapping("/orders/purchase")
    public ResponseEntity<Map<String, Object>> purchase(@RequestBody BuyDo buyDo) {
        Map<String, Object> result = new HashMap<>();

        try {
            int rows = buyService.purchase(buyDo);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "购买成功");
                result.put("orderId", buyDo.getOrderId()); // 返回订单ID
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "库存不足，购买失败");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
            }
        } catch (IllegalArgumentException e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统异常，请稍后重试");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}